import { Router } from 'express';
import sequelize from '../config/database.js';
import { logger } from '../utils/logger.js';
import browserRoutes from './browser.routes.js';

const router = Router();

router.use('/browser', browserRoutes);

// 健康检查
router.get('/health', (req, res) => {
  res.json({
    status: 'ok',
    timestamp: new Date().toISOString()
  });
});

// 数据库连接测试
router.get('/db-test', async (req, res) => {
  try {
    await sequelize.authenticate();
    const dbConfig = {
      host: process.env.MYSQL_HOST,
      port: process.env.MYSQL_PORT,
      database: process.env.MYSQL_DATABASE,
      username: process.env.MYSQL_USERNAME,
    };
    logger.info('数据库连接成功', dbConfig);
    res.json({
      success: true,
      message: '数据库连接成功',
      config: dbConfig
    });
  } catch (error) {
    logger.error('数据库连接失败:', {
      error: error.message,
      stack: error.stack
    });
    res.status(500).json({
      success: false,
      error: error.message,
      details: process.env.NODE_ENV === 'development' ? error.stack : undefined
    });
  }
});

export default router;
